package com.zjsd.sqeybusiness.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.zjsd.sqeybusiness.dto.CompanyInfoDTO;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.zjsd.sqeybusiness.domain.CompanyInfo;
import com.zjsd.sqeybusiness.service.ICompanyInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.TokenService;

/**
 * 企业信息Controller
 *
 * @author Zheng
 * @date 2020-11-05
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/jph/companyInfo")
public class CompanyInfoController extends BaseController
{
    @Autowired
    private ICompanyInfoService companyInfoService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysDeptService deptService;


    /**
     * 查询企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('jph:companyInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyInfo companyInfo)
    {
        startPage();
        List<CompanyInfo> list = companyInfoService.selectCompanyInfoList(companyInfo);
        return getDataTable(list);
    }

    /**
     * 导出企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('jph:companyInfo:export')")
    //@Log(title = "企业信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyInfo companyInfo)
    {
        List<CompanyInfo> list = companyInfoService.selectCompanyInfoList(companyInfo);
        ExcelUtil<CompanyInfo> util = new ExcelUtil<CompanyInfo>(CompanyInfo.class);
        return util.exportExcel(list, "companyInfo");
    }

    /**
     * 获取企业信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('jph:companyInfo:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable("companyId") Long companyId)
    {
        return AjaxResult.success(companyInfoService.selectCompanyInfoById(companyId));
    }

    /**
     * 新增企业信息
     */
    @PreAuthorize("@ss.hasPermi('jph:companyInfo:add')")
    //@Log(title = "企业信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyInfoDTO dto)
    {
        SysDept sysDept = deptService.selectDeptById(dto.getDeptId());
        System.out.println("sysdept:" + sysDept);
        if(sysDept != null){
            System.out.println("deptName:" + sysDept.getDeptName());
            dto.setName(sysDept.getDeptName());
        }
        companyInfoService.insertCompanyInfo(dto);
        return AjaxResult.success();
    }

    /**
     * 修改企业信息
     */
    @PreAuthorize("@ss.hasPermi('jph:companyInfo:edit')")
    //@Log(title = "企业信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyInfoDTO dto)
    {
        companyInfoService.updateCompanyInfo(dto);
        return AjaxResult.success();
    }

    /**
     * 删除企业信息
     */
    @PreAuthorize("@ss.hasPermi('jph:companyInfo:remove')")
    //@Log(title = "企业信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Long[] companyIds)
    {
        return toAjax(companyInfoService.deleteCompanyInfoByIds(companyIds));
    }

    /**
     * 获取企业信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('jph:companyInfo:query')")
    @GetMapping(value = "/getUserDeptInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        return AjaxResult.success(companyInfoService.selectDeptInfoByUserId(loginUser.getUser().getUserId()));
    }


    /**
     * 获取企业信息详细信息
     */
    @GetMapping(value = "/getCompanyInfoByDeptId")
    public AjaxResult getCompanyInfoByDeptId( String deptId)
    {
        return AjaxResult.success(companyInfoService.selectCompanyInfoByDeptId(deptId));
    }


}
